package org.unit.testmgmt.ui.generator.wizard;

import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.ui.JavaElementLabelProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableColumn;
import org.unit.testmgmt.generator.wizard.GeneratorWizardTestBases;
import org.unit.testmgmt.messages.Messages;
import org.unit.testmgmt.tml.UTMModel;
import org.unit.testmgmt.ui.table.TableViewerBase;

/**
 * The page for the test bases and the mock-methods.
 * 
 * @author Robert Streng
 *
 */
public class GeneratorWizardTestBasesPage extends GeneratorWizardBasePage {

	private ComboViewer cboConstructor;

	private Composite mainContainer;

	private Group grpTblTestbases;

	private Group grpTblMocks;

	private GeneratorWizardMainPage mainPage;

	public GeneratorWizardTestBasesPage(String title, String description,
			String pageName, UTMModel model, GeneratorWizardMainPage mainPage) {
		super(title, description, pageName, model);
		this.mainPage = mainPage;
	}

	@Override
	public void createControl(Composite parent) {
		mainContainer = new Composite(parent, SWT.FILL);
		mainContainer.setLayout(new GridLayout(2, false));
		setControl(mainContainer);

		// methods
		Label lblMethod = new Label(mainContainer, SWT.NONE);
		lblMethod.setText(Messages.GeneratorWizardTestBasesPage_Constructor);

		cboConstructor = new ComboViewer(mainContainer, SWT.NONE);
		cboConstructor.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		
		JavaElementLabelProvider labelProvider = new JavaElementLabelProvider(
				JavaElementLabelProvider.SHOW_DEFAULT
						| JavaElementLabelProvider.SHOW_RETURN_TYPE
						| JavaElementLabelProvider.SHOW_TYPE) {
			 @Override
			public String getText(Object element) {
				if (element instanceof IMethod)
					return super.getText(element);
				
				return element.toString();
			}
		};
		
		cboConstructor.setLabelProvider(labelProvider);

		// test-bases
		grpTblTestbases = new Group(mainContainer, SWT.SCROLLBAR_OVERLAY);
		grpTblTestbases.setText(Messages.GeneratorWizardTestBasesPage_testbases);

		grpTblTestbases.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 2, 1));
		grpTblTestbases.setLayout(new GridLayout(1, false));

		// mocks
		grpTblMocks = new Group(mainContainer, SWT.SCROLLBAR_OVERLAY);
		grpTblMocks.setText(Messages.GeneratorWizardTestBasesPage_mocks);

		grpTblMocks.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 2, 1));
		grpTblMocks.setLayout(new GridLayout(1, false));
	}

	@Override
	protected void createController(UTMModel model) {
		this.controller = new GeneratorWizardTestBases(model, this);
	}

	public ComboViewer getCboConstructor() {
		return cboConstructor;
	}

	public Composite getMainContainer() {
		return mainContainer;
	}

	public Group getGrpTblTestbases() {
		return grpTblTestbases;
	}

	public Group getGrpTblMocks() {
		return grpTblMocks;
	}

	public GeneratorWizardMainPage getMainPage() {
		return mainPage;
	}

	/**
	 * Layout the table test bases.
	 * 
	 * @param tblTestbases
	 */
	public void layoutTblTestbases(TableViewerBase tblTestbases) {
		for (TableColumn column : tblTestbases.getTable().getColumns()) {
			column.pack();
			if (column.getWidth() > 150) {
				column.setWidth(150);
			}
			else if (column.getWidth() < 50) {
				column.setWidth(50);
			}
		}
		
		GridData gd = (GridData) tblTestbases.getControl().getLayoutData();
		gd.minimumHeight = 150;
		gd.heightHint = 150;
		gd.horizontalIndent = 0;

		grpTblTestbases.layout();
		mainContainer.layout();
	}

	/**
	 * Layout the table mocks.
	 * 
	 * @param tblMocks
	 */
	public void layoutTblMocks(TableViewerBase tblMocks) {
		for (TableColumn column : tblMocks.getTable().getColumns()) {
			column.pack();
			if (column.getWidth() > 150) {
				column.setWidth(150);
			}
			else if (column.getWidth() < 50) {
				column.setWidth(50);
			}
		}
		
		GridData gd = (GridData) tblMocks.getControl().getLayoutData();
		gd.minimumHeight = 150;
		gd.heightHint = 150;
		gd.horizontalIndent = 0;

		grpTblMocks.layout();
		mainContainer.layout();
	}
	
	@Override
	public GeneratorWizardTestBases getController() {
		return (GeneratorWizardTestBases)controller;
	}

}
